import { requestUrl, version } from '../config';

export function upload(filePath, tip) {
//   if (tip == null || tip == "") {
//     my.showLoading({ content: "加载中..." });
//   } else {
//     my.showLoading({ content: tip });
//   }
  var promise = new Promise((resolve, reject, defaults) => {
    const userInfo = my.getStorageSync({key: 'userInfo'})
    let shopId = my.getStorageSync({key:'shopId'})
    if (!shopId) {
      shopId = my.getExtConfigSync().shopId;

    }
    console.log(shopId)
    my.uploadFile({
      url: requestUrl + '/upload/file',
      header:{
        ShopId: shopId.data,
        appVersion: version,
        Token: (userInfo.data && userInfo.data.token) ? userInfo.data.token : ''
      },
      name: 'file',
      fileType: 'image',
      filePath: filePath,
      success: (result) => {
        my.hideLoading();
        resolve(result.data);
      },
      fail: (err) => {
        console.log(err)
        if (err.data.code == 403) {
          my.removeStorageSync({
            key:'userInfo'
          })
        }
        if(err.data.message) {
          my.showToast({content: err.data.message});

        }

        my.hideLoading();
        reject(err);
      },
      complete: () => {
        my.hideLoading();
        defaults;
      }
    });
  });
  return promise;
}